<?php

defined('SYSPATH') or die('No direct script access.');

class Report_Pagination extends Kohana_Pagination {

	private $page_array = array();
	private $preffix;
	private $max_pagination = 25;
	private $min_offset = 5;

	public function __construct(array $config = array()) {
		if (isset($_POST["page"])) {
			$_GET["page"] = $_POST["page"];
		}
		parent::__construct($config);
		if(isset($_GET["page"])){
			$_GET["page"] = intval($_GET["page"]);
			for($i=max(array(1,($_GET["page"]-$this->min_offset)));$i<=min(array(($_GET["page"]+$this->min_offset),$this->total_pages));$i++) $this->page_array[$i] = $i;
		}
		if(array_key_exists('preffix', $config)){
			$this->preffix = $config['preffix'];
		}
		if($this->total_pages>$this->max_pagination){
			for($i=1;$i<=$this->min_offset;$i++) $this->page_array[$i] = $i;
			for($i=($this->total_pages-$this->min_offset+1);$i<=$this->total_pages;$i++) $this->page_array[$i] = $i;
			for($i=1;$i<=$this->total_pages;$i+=intval($this->total_pages/$this->max_pagination)) $this->page_array[$i] = $i;
			
		}else{
			for ($i = 1; $i <= $this->total_pages; $i++) $this->page_array[$i] = $i;
		}
		ksort($this->page_array);
	}

	public function render($view='report/pagination.tpl') {
		// Automatically hide pagination whenever it is superfluous
		if ($this->config['auto_hide'] === TRUE AND $this->total_pages <= 1)
			return '';
		if ($view === NULL) {
			// Use the view from config
			$view = $this->config['view'];
		}

		if (!$view instanceof Smarty_View) {
			// Load the view file
			$view = Smarty_View::factory($view);
		}
		
		$view->set(get_object_vars($this));

		// overriden to pass just necessary variables, plus array of pages
		return $view;
	}

}